package com.wswy.wzcx.widget.drawable

import android.content.Context
import android.graphics.*
import android.graphics.drawable.Drawable
import android.support.v4.content.ContextCompat
import com.blankj.utilcode.util.SizeUtils
import com.wswy.wzcx.R

class PolylineDrawable(private val context: Context) : Drawable() {

    private val paint= Paint(Paint.ANTI_ALIAS_FLAG).apply {

        style = Paint.Style.STROKE
        color = ContextCompat.getColor(context, R.color.textColorPrimary_15)
        strokeWidth = SizeUtils.dp2px(1f).toFloat()
    }

    private val rect = Rect()

    private val path = Path()

    private val tmpRectF = RectF()

    override fun onBoundsChange(bounds: Rect?) {
        super.onBoundsChange(bounds)
        bounds?.let {
            rect.set(bounds)


            tmpRectF.set(rect)

            //tmpRectF.inset(paint.strokeWidth,paint.strokeWidth)

            path.reset()

            path.moveTo(0f,0f)
            path.lineTo(0f,tmpRectF.centerY()-rect.width())
            path.lineTo(tmpRectF.width(),tmpRectF.centerY())
            path.lineTo(0f,tmpRectF.centerY()+rect.width())
            path.lineTo(0f,tmpRectF.height())


        }
    }

    override fun draw(canvas: Canvas?) {
        canvas?.let {
            it.drawPath(path, paint)
        }
    }

    override fun setAlpha(alpha: Int) {
        paint.alpha = alpha
    }

    override fun getOpacity(): Int = PixelFormat.TRANSLUCENT

    override fun setColorFilter(colorFilter: ColorFilter?) {
        paint.colorFilter = colorFilter
    }

    override fun getIntrinsicHeight(): Int {
        return rect.height()
    }

    override fun getIntrinsicWidth(): Int {
        return rect.width()
    }

}